package com.ming.dancing.springboot.controller;


import com.alibaba.fastjson.JSON;
import com.ming.dancing.springboot.entity.Dancer;
import com.ming.dancing.springboot.entity.DancerQuery;
import com.ming.dancing.springboot.service.DancerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;




@Controller
public class DancerController {
    @Autowired
    DancerService dancerService;

//    @GetMapping("/dance/{id}")
//    @ResponseBody
//    public Dancer getDancer(@PathVariable("id") Integer id)
//    {
//        Dancer dancer = dancerDao.findById(id).orElse(null);
//        System.out.println(dancer);
//        return  dancer;
//    }

//    @GetMapping("/user")
//    public Dancer insertDancer(Dancer dancer)
//    {
//        Dancer save= dancerDao.save(dancer);
//        return  save;
//    }

    //查询所有舞者返回列表页面
    @GetMapping("/emps")
    public String list(Model model){
        List<Dancer> dancers = dancerService.findallDancers();
        //System.out.println("查询结果"+dancers);
        //放在请求域中
        model.addAttribute("emps",dancers);
        // thymeleaf默认就会拼串
        // classpath:/templates/xxxx.html
        return "dancer/alldancer";
    }

    //舞者添加页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //来到添加页面,查出所有的部门，在页面显示
        return "dancer/add";
    }
    //舞者添加
    //SpringMVC自动将请求参数和入参对象的属性进行一一绑定；要求请求参数的名字和javaBean入参的对象里面的属性名是一样的
    @PostMapping("/emp")
    public String addEmp(Dancer dancer){
        //来到员工列表页面

        //System.out.println("保存的信息："+dancer);
        //保存
        dancer.setCoursecount(0);
        dancerService.adddancer(dancer);
        // redirect: 表示重定向到一个地址  /代表当前项目路径
        // forward: 表示转发到一个地址
        return "redirect:/emps";
    }
    //查找一个舞者
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id,Model model){
        Dancer onedancer = dancerService.findDancerById(id);
        System.out.println("查到的舞者"+onedancer);
        model.addAttribute("emp",onedancer);

        //回到修改页面(add是一个修改添加二合一的页面);
        return "dancer/add";
    }

    //舞者修改；需要提交舞者id；
    @PutMapping("/emp")
    public String updateDancer(Dancer dancer){
        System.out.println("修改的员工数据："+dancer);
        dancerService.UpdateDancer(dancer);
        return "redirect:/emps";
    }
    //删除舞者
    @DeleteMapping("/emp/{id}")
    public String deleteDance(@PathVariable("id") Integer id)
    {
        System.out.println("删除的员工是"+id);
        dancerService.DeleteDancer(id);
        return "redirect:/emps";
    }
    //分页查询有条件的
    @RequestMapping(value = "/findDancerQuery",method = {RequestMethod.GET,RequestMethod.POST})
    public String findDancerQuery(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "0")Integer page,
                                  @RequestParam(value = "size",defaultValue = "5")Integer size, DancerQuery dancerQuery)
    {
        Page<Dancer> datas=dancerService.findDancerCriteria(page,size,dancerQuery);
        modelMap.addAttribute("datas",datas);
        System.out.println("查询结果为"+datas);
        return "dancer/alldancerPage";
    }




}
